package com.zsc.projectmanagementapi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zsc.projectmanagementapi.dao.EmployeeInformationMapper;
import com.zsc.projectmanagementapi.pojo.Do.EmployeeInformation;
import com.zsc.projectmanagementapi.pojo.Vo.EmployeeInformationVo;
import com.zsc.projectmanagementapi.service.EmployeeInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LinHj
 * @date 2021/6/10 12:59
 **/
@Service
public class EmployeeInformationServiceImpl implements EmployeeInformationService {

    @Autowired
    private EmployeeInformationMapper employeeInformationMapper;
    @Autowired
    private RedisTemplate redisTemplate;

//    @Override
//    public PageInfo<EmployeeInformation> getAllEmployeeInformation(int page, int count) {
//        PageHelper.startPage(page,count);
//        QueryWrapper<EmployeeInformation> employeeInformationQueryWrapper = new QueryWrapper<>();
//        List<EmployeeInformation> employeeInformations =
//                employeeInformationMapper.selectList(employeeInformationQueryWrapper);
//        PageInfo<EmployeeInformation> employeeInformationPageInfo = new PageInfo<>(employeeInformations);
//        return employeeInformationPageInfo;
//    }

    @Override
    @Cacheable(value = "employee_information",key = "'employee_' + #id")
    public EmployeeInformation getEmployeeInformationById(String id) {
        QueryWrapper<EmployeeInformation> employeeInformationQueryWrapper = new QueryWrapper<>();
        employeeInformationQueryWrapper.eq("id",id);
        EmployeeInformation employeeInformation = employeeInformationMapper.selectOne(employeeInformationQueryWrapper);
        return employeeInformation;
    }

    @Override
    public int deleteEmployee(String id) {
        return employeeInformationMapper.deleteById(id);
    }

    @Override
    public int updateEmployeeInformation(EmployeeInformation employeeInformation) {
        int i = employeeInformationMapper.updateById(employeeInformation);
        redisTemplate.delete("employee_"+employeeInformation.getId());
        return i;

    }

    @Override
    public List<EmployeeInformation> getAllEmployeeInformation(String name, Long jobId, String position) {
//        PageHelper.startPage(page,count);
        QueryWrapper<EmployeeInformation> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("delete",0);
        if (name != null){
//            queryWrapper.eq("name",name);
            queryWrapper.like("name",name);
        }
        if (jobId != null){
            queryWrapper.eq("job_id",jobId);
        }
        if (position != null){
            queryWrapper.eq("position",position);
        }
        List<EmployeeInformation> employeeInformations = employeeInformationMapper.selectList(queryWrapper);
        return employeeInformations;
    }

}
